package com.per.mybootall.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/5/19
 * Time: 15:30
 * Description:
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
  private static ApplicationContext applicationContext = null;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContextHolder.applicationContext = applicationContext;
  }

  public static <T> T getBean(Class<T> type) {
    assertContextInjected();
    return applicationContext.getBean(type);
  }
  public static <T> T getBean(String name,Class<T> type) {
    assertContextInjected();
    return applicationContext.getBean(name,type);
  }

  public static void assertContextInjected() {
    if (applicationContext == null) {
      throw new RuntimeException("applicationContext未注入");
    }
  }
}